﻿using BingSpatialDataServiceExcelAddin.BSDSTools.Models;
using System;
using System.Linq;
using System.Windows.Forms;
using System.Xml.Linq;

namespace BingSpatialDataServiceExcelAddin.Views
{
    public partial class SettingsForm : Form
    {
        public SettingsForm()
        {
            InitializeComponent();

            Settings settings = new Settings(Globals.ThisAddIn.Application.ActiveWorkbook);

            if (string.IsNullOrWhiteSpace(settings.Culture))
            {
                settings.Culture = "en-US";
            }

            DSNameTbx.Text = settings.DataSourceName;
            EntityTypeTbx.Text = settings.EntityTypeName;
            MasterKeyTbx.Text = settings.MasterKey;
            QueryKeyTbx.Text = settings.QueryKey;
            MakePublicChbx.Checked = settings.IsPublic;

            XDocument doc = XDocument.Load(new System.IO.StreamReader(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("BingSpatialDataServiceExcelAddin.BSDSTools.Cultures.xml")));

            var cultures = (from elem in doc.Descendants("Culture")
                            select new Culture
                            {
                                ID = elem.Attribute("ID").Value,
                                Name = elem.Attribute("Name").Value
                            }).ToList<Culture>();

            CulturesCbx.DataSource = cultures;
            CulturesCbx.DisplayMember = "Name";
            CulturesCbx.SelectedItem = (from x in cultures
                                       where string.Compare(x.ID, settings.Culture, true) == 0
                                       select x).FirstOrDefault();
        }

        private void OkBtn_Click(object sender, EventArgs e)
        {
            Settings settings = new Settings(Globals.ThisAddIn.Application.ActiveWorkbook);

            settings.DataSourceName = DSNameTbx.Text;
            settings.EntityTypeName = EntityTypeTbx.Text;
            settings.MasterKey = MasterKeyTbx.Text;
            settings.QueryKey = QueryKeyTbx.Text;
            settings.IsPublic = MakePublicChbx.Checked;

            if (string.IsNullOrWhiteSpace(settings.DataSourceName))
            {
                MessageBox.Show("Invalid Data Source Name Entry.");
                return;
            }

            if (string.IsNullOrWhiteSpace(settings.EntityTypeName))
            {
                MessageBox.Show("Invalid Entity Type Name Entry.");
                return;
            }

            if (string.IsNullOrWhiteSpace(settings.MasterKey))
            {
                MessageBox.Show("Invalid Master Key Entry.");
                return;
            }

            if (CulturesCbx.SelectedItem != null)
            {
                settings.Culture = (CulturesCbx.SelectedItem as Culture).ID;
            }

            //Save Settings
            settings.Save(Globals.ThisAddIn.Application.ActiveWorkbook);

            this.Close();
        }

        private void CancelBtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
